<?php

class MarkupsController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	//public $layout='//layouts/column2';
		public $layout='column1';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','admin','updatee'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('delete'),
				'users'=>array('admin','gracheva','chanov','popova'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Markups;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Markups']))
		{
			$model->attributes=$_POST['Markups'];
			if($model->save())
				//$this->redirect(array('view','id'=>$model->id));
				$this->redirect(array('/markups/admin'));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Markups']))
		{
			$model->attributes=$_POST['Markups'];
			if($model->save())
			//----- begin new code --------------------
	            if (!empty($_GET['asDialog']))
	            {
	                //Close the dialog, reset the iframe and update the grid
	                echo CHtml::script("window.parent.$('#cru-dialog').dialog('close');window.parent.$('#cru-frame').attr('src','');window.parent.$.fn.yiiGridView.update('{$_GET['gridId']}');");
	                Yii::app()->end();
	            }
	            else
	        //----- end new code --------------------			
				//$this->redirect(array('view','id'=>$model->id));
				$this->redirect(array('/markups/admin'));
		}
		
		//----- begin new code --------------------
	    if (!empty($_GET['asDialog']))
	        $this->layout = '//layouts/iframe';
	    //----- end new code --------------------		

		$this->render('update',array(
			'model'=>$model,
		));
	}
	
	public function actionUpdatee()
	{	
		$es = new EditableSaver('Markups'); 
		$es->update();		
	}	

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Markups');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Markups('search');
		$model->unsetAttributes();  // clear any default values
		
		if (isset($_GET['pageSize'])) {
			Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
			unset($_GET['pageSize']);
        }
		
		if(isset($_GET['Markups']))
			$model->attributes=$_GET['Markups'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Markups::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='markups-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	protected function checkResult($data,$row)
	{
		$res = '';
		switch ($data->name) {
			case 'sst':
				$res = "ССТ";
				break;
			case 'ek-pe':
				$res = "ПЭ";
				break;
			case 'ek-ek':
				$res = "ЭК";
				break;
			case 'jer':
				$res = "ЖЭР";
				break;				
			case 'ek-se':
				$res = "ЭК.Силовая электроника";
				break;
			case 'ek-dk':
				$res = "ЭК.Датаком";
				break;
		}
		return $res;
	}
	
	protected function checkResult2($data,$row)
	{
		$res = '';
		switch ($data->manager) {
			case 'popova':
				$res = "Попова";
				break;
			case 'smirnova':
				$res = "Смирнова";
				break;	
			case 'platceva':
				$res = "Платцева";
				break;
			case 'jivova':
				$res = "Живова";
				break;	
			case 'denisov':
				$res = "Денисов";
				break;					
			case 'baraeva':
				$res = "Бараева";
				break;
			case 'fattahova':
				$res = "Фаттахова";
				break;
			case 'vasilyeva':
				$res = "Васильева";
				break;				
			case 'editors':
				$res = "Редакционная";
				break;
			case 'orders':
				$res = "Заказная";
				break;
		}
		return $res;
	}	
	
	protected function checkResult3($data,$row)
	{
		$res = '';
		switch ($data->types) {
			case '1/8':
				$res = "1/8 м";
				break;
			case '1/6':
				$res = "1/6 м";
				break;
			case '1/4':
				$res = "1/4 м";
				break;
			case '1/3':
				$res = "1/3 м";
				break;
			case '1/2':
				$res = "1/2 м";
				break;
			case '1/1':
				$res = "1/1 м";
				break;
			case '2/1':
				$res = "2/1 м";
				break;
			case 'stick':
				$res = "Вклейка";
				break;
			case 'cover':
				$res = "Обложка";
				break;		
			case 'news':
				$res = "Новость";
				break;
			case 'nk':
				$res = "НК";
				break;
			case 'table':
				$res = "Таблица";
				break;
			case 'logo1':
				$res = "Лого в таблице";
				break;
			case 'logo2':
				$res = "Лого в указателе";
				break;					
			case 'bonus':
				$res = "Статья-бонус";
				break;
			case 'promo':
				$res = "Статья-рекламная";
				break;
			case 'pr':
				$res = "Статья-пиар";
				break;
			case 'edit':
				$res = "Статья редакционная";
				break;
			case 'translate':
				$res = "Перевод";
				break;
			case 'author':
				$res = "Авторский материал";
				break;
			case 've':
				$res = "Обзор на ВЭ";
				break;
			case 'lighting':
				$res = "Обзор на Лайтинг";
				break;
			case 'elcom':
				$res = "Обзор на Элком";
				break;
		}
		return $res;
	}
	
	protected function checkResult4($data,$row)
	{
		$res = '';
		switch ($data->conditions) {
			case 'work':
				$res = "В работе";
				break;
			case 'sended':
				$res = "Отправлено";
				break;
		}
		return $res;
	}
	
	protected function checkResult5($data,$row)
	{
		$res = '';
		switch ($data->editor) {
			case 'andreev':
				echo "Андреев";
				break;
			case 'voronina':
				echo "Воронина";
				break;
			case 'gracheva':
				echo "Грачева";
				break;
			case 'gasanov':
				echo "Гасанов";
				break;
			case 'ejov':
				echo "Ежов";
				break;
			case 'zabolot':
				echo "Заболотная";
				break;
			case 'kalachov':
				echo "Калачов";
				break;
			case 'mainstrenko':
				echo "Майстренко";
				break;
			case 'novikov':
				echo "Новиков";
				break;
			case 'fomichev':
				$res = "Фомичев";
				break;				
			case 'chanov':
				echo "Чанов";
				break;
			case 'simonov':
				echo "Симонов";
				break;
			case 'kishov':
				echo "Кишов";
				break;
			case 'samkova':
				echo "Самкова";
				break;				
		}
		return $res;
	}	
	
	protected function checkFiles($data,$row)
	{
		$res = '';
		$list = explode('|', $data->filename); 
		foreach ($list as $file) {
			$res .= '<a href="/images/files/'.$file.'">'.$file .'</a>'.'<br>';
		}
		return $res;
	}	
}
